package com.ld.security.shop.crawler.modules.re.crawler.edd;

import com.ld.security.common.core.util.JsonUtil;
import com.ld.security.shop.crawler.modules.re.constant.CrawlerConstant;
import com.ld.security.shop.crawler.modules.re.crawler.BaseCrawler;
import com.ld.security.shop.crawler.modules.re.entity.ReDetails;
import com.ld.security.shop.crawler.modules.re.entity.RePage;
import com.ld.security.shop.crawler.modules.re.service.IReDetailsService;
import com.ld.security.shop.crawler.modules.re.service.IRePageService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.List;
import java.util.Map;

@Component
public class EddRePageCrawler extends BaseCrawler<RePage> {

    @Autowired
    IRePageService iRePageService;

    @Autowired
    IReDetailsService iReDetailsService;

    @Autowired
    EddReDetailsCrawler eddReDetailsCrawler;

    public EddRePageCrawler() {
        super(CrawlerConstant.projectCode.edd);
    }

    @Override
    public RePage dataCrawler(String url) {
        RePage page = super.dataCrawler(url);

        String preUrl = changeUrl(url,false);
        String nextUrl = changeUrl(url,true);

        page.setPreUrl(preUrl);
        page.setNextUrl(nextUrl);
        iRePageService.save(page);

       Map data =  JsonUtil.toObject( page.getContent(),Map.class);
        List<Map> list =  getValue(data,"data","saleList");
        for (Map map : list) {
            String id = map.get("saleId").toString();
            String detailsUrl="http://e.dangdang.com/products/"+id+".html";
            ReDetails reDetails = eddReDetailsCrawler.dataCrawler(detailsUrl);
            iReDetailsService.save(reDetails);
        }

        return page;
    }




}

